package com.xmall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmall.annotation.LoginRequiredFalse;
import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.es.EsSkuDto;
import com.xmall.product.define.dto.spu.SpuInfoDto;
import com.xmall.product.define.dto.spu.SpuResponseDto;
import com.xmall.product.define.request.spu.SpuListPageQueryRequest;
import com.xmall.product.define.request.spu.SpuUpdateRequest;
import com.xmall.product.service.ISpuInfoService;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品spu信息
 */
@RestController
@RequestMapping("/service/product/spu")
public class SpuInfoController {

    @Resource
    private ISpuInfoService spuInfoService;

    /**
     * 分页列表
     */
    @GetMapping("/listPage")
    public Result<Page<SpuInfoDto>> listPage(@Validated SpuListPageQueryRequest query){
        Page<SpuInfoDto> pageList = spuInfoService.listPage(query);
        return Result.success(pageList);
    }

    /**
     * 详情
     */
    @LoginRequiredFalse
    @GetMapping("/apiInfo/{id}")
    public Result<SpuResponseDto> apiInfo(@PathVariable("id") Long id) throws Exception{
        SpuResponseDto dto = spuInfoService.apiInfo(id);
        return null != dto ? Result.success(dto) : Result.error();
    }

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public Result<SpuResponseDto> info(@PathVariable("id") Long id) throws Exception{
        SpuResponseDto dto = spuInfoService.info(id);
        return null != dto ? Result.success(dto) : Result.error();
    }

    /**
     * 新增
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PreAuthorize("hasAuthority('product:spu:save')")
    @PostMapping("/save")
    public Result save(@Validated(value = AddGroup.class) @RequestBody SpuUpdateRequest request){
        spuInfoService.saveEntity(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('product:spu:update')")
    @PostMapping("/update")
    public Result update(@Validated(value = UpdateGroup.class) @RequestBody SpuUpdateRequest request){
        spuInfoService.updateEntity(request);
        return Result.success();
    }

    /**
     * 删除
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('product:spu:delete')")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        spuInfoService.deleteEntity(id);
        return Result.success();
    }

    /**
     * 构造es对象
     */
    @LoginRequiredFalse
    @GetMapping("/buildEsSku")
    public Result<List<EsSkuDto>> buildEsSku(@RequestParam("spuId") Long spuId,
                                             @RequestParam(value = "esVersion", required = false) Integer esVersion) {
        return Result.success(spuInfoService.buildEsSku(spuId, esVersion));
    }
}
